package Testprogram;
/*
This is class for calculating factorial of n,
with and without recursion method
*/
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.InputMismatchException;

public class factorial{
	private static int count = 0;
	
	public static void main(String [] args){
		try{
		System.out.println("Insert one integer number");
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		long result_non_recur = factorialSimple(num);
		//memoryTest();
		long result_recur = factorialRecur(num);
		//memoryTest();
		
		System.out.println("Simple Factorial of "+ num+ " is "+ result_non_recur);
		
		System.out.println("Recursive Factorial of " + num + " is " + result_recur);
		
		System.out.println("number of step to calculate recursively: "+ count);
		
		}catch(InputMismatchException e){
			System.out.println("Please insert interger number");
		}
	}
	
	public static long factorialSimple(int num){
		long result = 1;
		for(int i = 1; i <= num; i++){
			result *= i;
			count++;
		}
		System.out.println("number of step to calculate non-recursive factorial: "+count);
		count=0;
		return result;
	}
	
	public static long factorialRecur(int num){
		long result = 1;
		count++;
		if (num == 1){
			return 1;
		}else{
			result= (long)num * factorialRecur(num - 1);
		}
		return result;
	}
	
	public static void memoryTest(){
		Runtime runtime = Runtime.getRuntime();

	    NumberFormat format = NumberFormat.getInstance();

	    StringBuilder sb = new StringBuilder();
	    long maxMemory = runtime.maxMemory();
	    long allocatedMemory = runtime.totalMemory();
	    long freeMemory = runtime.freeMemory();

	    sb.append("free memory: " + format.format(freeMemory / 1024) + "\n");
	    sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "\n");
	    sb.append("max memory: " + format.format(maxMemory / 1024) + "\n");
	    sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "\n");
	    System.out.println(sb);
	}

}